package helloWorld;

import helloWorld.infoRetriever.Album;
import processing.core.PImage;
import helloWorld.infoRetriever.OnlineInfoRetriever;
import helloWorld.musicplayer.Mp3Player;
import helloWorld.musicplayer.Mp3Scraper;

import java.util.List;
import java.util.Vector;

import org.mt4j.MTApplication;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.font.IFont;
import org.mt4j.components.visibleComponents.widgets.MTImage;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.zoomProcessor.ZoomProcessor;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;
import org.mt4j.util.math.Vertex;

import processing.core.PImage;

public class AlbumInfoScene extends AbstractScene {

	private MTImage goBackButton;
	private MTImage mapsButton;
	
	public AlbumInfoScene(MTApplication mtApplication, String name) {
		super(mtApplication, name);
		this.getCanvas().setFrustumCulling(true);
		this.setClearColor(new MTColor(146, 150, 188, 255));
		this.mtApplication = mtApplication;
		myGL = new AlbumInfoGestureListener(this.getMTApplication(), this);
		tracks = new Vector<AlbumTrack>();
		
		this.getCanvas().setName("artistinfocanvas");
		
		
		String goBackUrl = "http://clipart.usscouts.org/library/BSA_Cub_Scouts/Cub_insignia/arrow_point_clipart_bw.gif";
		PImage pim = this.getMTApplication().loadImage(goBackUrl);
		goBackButton = new MTImage(pim, this.getMTApplication());
		goBackButton.setSizeXYGlobal(60, 60);
		goBackButton.setName("albuminfosceneBACK");

		this.getCanvas().addChild(goBackButton);
		goBackButton.setPositionGlobal(new Vector3D(getMTApplication().width/2, getMTApplication().height*9/10));
	
		String globeUrl = "http://www.public-domain-photos.com/free-cliparts-4/other/geography/svg_globe.png";
		PImage pim2 = this.getMTApplication().loadImage(globeUrl);
		mapsButton = new MTImage(pim2, this.getMTApplication());
		mapsButton.setSizeXYGlobal(60, 60);
		mapsButton.setName("GLOBEBUTTON");
		this.getCanvas().addChild(mapsButton);
		mapsButton.setPositionGlobal(new Vector3D(7*getMTApplication().width/8, getMTApplication().height*9/10));
	}

	@Override
	public void init() {
		initMembers();
		initGui();
		getCanvas().registerInputProcessor(new ZoomProcessor(getMTApplication()));
		getCanvas().addGestureListener(ZoomProcessor.class, myGL);

		goBackButton.unregisterAllInputProcessors();
		goBackButton.registerInputProcessor(new TapProcessor(getMTApplication()));
		goBackButton.addGestureListener(TapProcessor.class, myGL);
		
		mapsButton.unregisterAllInputProcessors();
		mapsButton.registerInputProcessor(new TapProcessor(getMTApplication()));
		mapsButton.addGestureListener(TapProcessor.class, myGL);
		
	}

	@Override
	public void shutDown() {
		getCanvas().unregisterAllInputProcessors();
	}

	private void initMembers() {
		Mp3Scraper scraper = new Mp3Scraper();
		OnlineInfoRetriever retriever = new OnlineInfoRetriever();

		this.albumArtist = Controller.getInstance().getCurrentArtist();
		this.albumTitle = Controller.getInstance().getCurrentAlbum();

		//this.albumArtist = "Coldplay";
		//this.albumTitle = "Parachutes";

		// artiest specifieren
		retriever.retrieveArtist(albumArtist);
		// albumobject ophalen
		this.currentAlbum = retriever.addAlbum(albumTitle);
		// url's naar tracks ophalen
		Vector<String> albumTrackLinks = scraper.getMp3List(this.albumArtist,
				this.albumTitle);
		// Mp3Player aanmaken
		if (albumTrackLinks != null) {
			this.currentPlayer = new Mp3Player();
			this.currentPlayer.setPlayList(albumTrackLinks);
		}
		else
			System.out.println( "Scraper Error!!" );
		this.albumTracks = currentAlbum.getOrderedTracks();
		this.albumImage = currentAlbum.getImage();
		this.albumInfo = currentAlbum.getInfo();
	}

	private void initGui() {
		int hPos, vPos;
		// titel weergeven
		MTTextArea textArea = new MTTextArea(mtApplication, FontManager
				.getInstance().createFont(mtApplication, "arial.ttf", 25,
						new MTColor(0, 0, 0), new MTColor(0, 0, 0)));
		textArea.setText(albumArtist + " - " + albumTitle);
		hPos = 430;
		vPos = 25;
		textArea.setPositionGlobal(new Vector3D(hPos, vPos));
		this.getCanvas().addChild(textArea);

		// albumafbeelding weergeven
		PImage img = getMTApplication().loadImage(albumImage);
		MTImage mtimage = new MTImage(img, getMTApplication());
		mtimage.setWidthXYGlobal(widthImage);
		mtimage.setHeightXYGlobal(heightImage);
		hPos = 130;
		vPos = 130;
		mtimage.setPositionGlobal(new Vector3D(hPos, vPos));
		this.getCanvas().addChild(mtimage);

		// albuminfo tonen
		IFont font2 = FontManager.getInstance().createFont(
				this.getMTApplication(), "arial.ttf", 17, // Font size
				new MTColor(0, 0, 0), // Font fill color
				new MTColor(0, 0, 0));
		MTTextArea albuminfoArea = new MTTextArea(this.getMTApplication(),
				font2);
		hPos = 25;
		vPos = 600;
		this.getCanvas().addChild(albuminfoArea);
		albuminfoArea.setPositionGlobal(new Vector3D(hPos, vPos));
		// albuminfoArea.setFillColor(new MTColor(146, 150, 188, 255));
		albuminfoArea.setWidthLocal(400);
		albuminfoArea.setHeightLocal(200);
		albuminfoArea.setText(updateString(albumInfo));

		// lijst van tracks afgaan en gui-elementen aanmaken
		for (int i = 0; i <albumTracks.size(); i++)
			addTrack(albumTracks.get(i), i);
	}

	// dient in volgorde geadd te worden
	private void addTrack(String trackName, int i) {
		AlbumTrack newTrack = new AlbumTrack(this.getMTApplication(), font);
		newTrack.setNoFill(true);
		newTrack.setNoStroke(true);
		newTrack.setText(trackName);
		newTrack.setName(""+i);
		this.getCanvas().addChild(newTrack);
		newTrack.unregisterAllInputProcessors();
		newTrack.registerInputProcessor(new TapProcessor(this.getMTApplication()));
		newTrack.addGestureListener(TapProcessor.class, myGL);
		newTrack.setPositionGlobal(new Vector3D(this.getMTApplication().width / 5f, (i + 1)* this.getMTApplication().height / 13f));
		tracks.add(newTrack);
	}

	private String updateString(String string) {
		String artInf = string;
		String formatedInfo = "";
		boolean captureChar = true;
		int position = 0;
		for (int i = 0; i < artInf.length(); i++) {
			if (position % 50 == 0) {
				formatedInfo += '\n';
			}

			if (artInf.charAt(i) == '<')
				captureChar = false;

			if (artInf.charAt(i) == '>') {
				captureChar = true;
				continue;
			}

			if (captureChar) {
				formatedInfo += artInf.charAt(i);
				position++;
			}
		}
		// clean out \n's
		String formatedInfo2 = "";
		char lastChar = formatedInfo.charAt(0);
		for (int i = 1; i < formatedInfo.length(); i++) {
			if (formatedInfo.charAt(i) == '\n' && lastChar == '\n')
				;
			else {
				formatedInfo2 += formatedInfo.charAt(i);
			}
			lastChar = formatedInfo.charAt(i);
		}
		return formatedInfo;
	}
	
	public void play (int id){
		if(this.currentPlayer == null)
			System.out.println("player null");
		else
			this.currentPlayer.playSong(id);
	}
	private MTApplication mtApplication;
	private Album currentAlbum = null;
	private Mp3Player currentPlayer = null;
	private String albumTitle;
	private String albumArtist;
	private String albumImage;
	private String albumInfo;
	private List<String> albumTracks;
	private static final int widthImage = 260;
	private static final int heightImage = 260;
	private final IFont font = FontManager.getInstance().createFont(
			this.getMTApplication(), "arial.ttf", 12, // Font size
			new MTColor(0, 0, 0), // Font fill color
			new MTColor(0, 0, 0));
	private AlbumInfoGestureListener myGL;
	private Vector<AlbumTrack> tracks;
}
